package com.appcenter.message.controller;


import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import com.appcenter.message.input.dingconfig.*;
import com.appcenter.message.service.DingConfigsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/services/jzmessagecenterservice/dingconfigs")
@Api(value="消息钉钉配置API",description = "消息钉钉配置API",produces="application/json")
public class DingConfigController {
    @Autowired
    DingConfigsService dingConfigsService;


    @ApiOperation(value = "创建钉钉配置", notes = "创建钉钉配置",produces = "application/json")
    @PostMapping("/createdingconfigs")
    public ResponseEntity<JsonResult> createDingConfig(@RequestBody CreateDingConfigInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setSuccess(true);
            r.setResult(dingConfigsService.CreateDingConfig(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "删除钉钉配置", notes = "删除钉钉配置",produces = "application/json")
    @PostMapping("/deletedingconfigs")
    public ResponseEntity<JsonResult> deleteDingConfig(@RequestBody DeleteDingConfigInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(dingConfigsService.DeleteDingConfig(input));

        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "修改钉钉配置", notes = "修改钉钉配置",produces = "application/json")
    @PostMapping("/editdingconfigs")
    public ResponseEntity<JsonResult> editDingConfig(@RequestBody EditDingConfigInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(dingConfigsService.EditDingConfig(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "搜索钉钉配置", notes = "搜索钉钉配置",produces = "application/json")
    @PostMapping("/getdingconfigss")
    public ResponseEntity<JsonResult> getDingConfigs(@RequestBody GetDingConfigsInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();

        try {
            r.setResult(dingConfigsService.GetDingConfigs(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "加载钉钉配置信息", notes = "加载钉钉配置信息",produces = "application/json")
    @PostMapping("/pageLoad")
    public ResponseEntity<JsonResult> pageLoad(@RequestBody PageLoadInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(dingConfigsService.PageLoad(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

}
